<?php

namespace app\api\model;

use think\Model;

class Proof extends Model
{

    protected $add_rule =   [
        'batch_id'   => 'require',
        'proof_time'   => 'require',
        'name'       =>'require',
        'organ'       =>'require',
        'user' =>'require',
        'standard'       =>'require',
        'situation' =>'require',
    ];

    protected $add_message  =   [
        'batch_id.require' => '所属批次不能为空',
        'proof_time.require' => '请输入检验时间',
        'name.require' => '请输入检验名称',
        'organ.require' => '请输入检验机构',
        'user.require' => '请输入检验员',
        'standard'       =>'请输入执行标准',
        'situation' =>'请输入检验情况',
    ];

    public function add_vali($data)
    {


        try {
            validate($this->add_rule,$this->add_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function add($data)
    {
        $data['time'] = date('Y-m-d');
        $this->startTrans();
        try {
            $this->allowField([
                'batch_id',
                'proof_time',
                'time',
                'user_id',
                'status',
                'name',
                'organ',
                'user',
                'standard',
                'situation',
                'result',
                'images',
                'content',
                'files',
                'type_name',
                'ls_dj',
                'ls_dj_images',
                'yj_images',
                'yj_is',
                'ls_is'
            ])->save($data);
            $this->commit();
            return re_success("添加成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $edit_rule =   [
        'id'   => 'require',
        'batch_id'   => 'require',
        'proof_time'   => 'require',
        'name'       =>'require',
        'organ'       =>'require',
        'user' =>'require',
        'standard'       =>'require',
        'situation' =>'require',
    ];

    protected $edit_message  =   [
        'id.require'=>'缺失的用户',
        'batch_id.require' => '所属批次不能为空',
        'proof_time.require' => '请输入检验时间',
        'name.require' => '请输入检验名称',
        'organ.require' => '请输入检验机构',
        'user.require' => '请输入检验员',
        'standard'       =>'请输入执行标准',
        'situation' =>'请输入检验情况',
    ];
    public function edit_vali($data)
    {

        try {
            validate($this->edit_rule,$this->edit_message)->check($data);

        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    protected $edit_status_rule =   [
        'id'   => 'require',
        'batch_id'   => 'require',
        'status'   => 'require'
    ];

    protected $edit_status_message  =   [
        'id.require'=>'缺失',
        'batch_id.require'=>'缺失的批次',
        'status.require' => '状态不能为空',
    ];
    public function edit_status_vali($data)
    {

        try {
            validate($this->edit_status_rule,$this->edit_status_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function editStatus($data)
    {

        $model = Proof::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField([
                'status'
            ])->save($data);
            $model->commit();
            return re_success("操作成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    public function edit($data)
    {
        $model = Proof::find($data['id']);
        $model->startTrans();
        try {

            $model->allowField([
                'proof_time',
                'time',
                'user_id',
                'status',
                'name',
                'organ',
                'user',
                'standard',
                'situation',
                'result',
                'images',
                'content',
                'files',
                'type_name',
                'ls_dj',
                'ls_dj_images',
                'yj_images',
                'yj_is',
                'ls_is'
            ])->save($data);
            $model->commit();
            return re_success("修改成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $dele_rule =   [
        'id'   => 'require',
        'batch_id'=>'require',
    ];

    protected $dele_message  =   [
        'id.require'=>'缺失的参数',
        'batch_id.require'=>'缺失的批次',
    ];
    public function dele_vali($data)
    {

        try {

            validate($this->dele_rule,$this->dele_message)->check($data);
            $info = $this->find($data['id']);
            if($info['batch_id'] !== $data['batch_id']){
                return re_warning('已记录非法删除他人批次');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function dele($id)
    {

        $model = Proof::find($id);

        $model->startTrans();
        try {
            $model->delete();
            $model->commit();
            return re_success("删除成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
}